package com.sttk.secreconeclick.bookkeep.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.lzb.basic.dialog.BaseSmartDialog;
import com.lzb.basic.utils.SPUtil;
import com.sttk.secreconeclick.bookkeep.Content;
import com.sttk.secreconeclick.bookkeep.R;
import com.sttk.secreconeclick.bookkeep.bean.BillBean;
import com.sttk.secreconeclick.bookkeep.databinding.DialogBillDetailBinding;
import com.sttk.secreconeclick.bookkeep.databinding.DialogChangeCycleBinding;

import java.text.SimpleDateFormat;

public class BillDetailDialog extends BaseSmartDialog<DialogBillDetailBinding> implements View.OnClickListener {

    private BillBean billBean;

    public BillDetailDialog(@NonNull Context context) {
        super(context);
    }

    public void setBillBean(BillBean billBean) {
        this.billBean = billBean;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_bill_detail;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvDelete.setOnClickListener(this);
        mDataBinding.tvChange.setOnClickListener(this);

        // 格式化金额保留两位小数
        String formattedMoney = String.format("%.2f", billBean.getMoney());
        if (billBean.isIncome()) {
            mDataBinding.tvMoney.setTextColor(Color.parseColor("#00B868"));
            mDataBinding.tvMoney.setText("+" + formattedMoney);
        } else {
            mDataBinding.tvMoney.setTextColor(Color.parseColor("#323433"));
            mDataBinding.tvMoney.setText("-" + formattedMoney);
        }
        mDataBinding.tvType.setText(billBean.getBillTypeBean().getName());
        mDataBinding.tvDate.setText(TimeUtils.millis2String(billBean.getDate(), new SimpleDateFormat("yyy-MM-dd")));
        if (TextUtils.isEmpty(billBean.getRemark())) {
            mDataBinding.llRemark.setVisibility(View.GONE);
        } else {
            mDataBinding.llRemark.setVisibility(View.VISIBLE);
            mDataBinding.tvRemark.setText(billBean.getRemark());
        }
        if (TextUtils.isEmpty(billBean.getImg())) {
            mDataBinding.llImg.setVisibility(View.GONE);
        } else {
            mDataBinding.llImg.setVisibility(View.VISIBLE);
            Glide.with(getContext()).load(billBean.getImg()).into(mDataBinding.ivImg);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvDelete) {
            dismiss();
            if (listener != null) {
                listener.clickDelete();
            }
        } else if (id == R.id.tvChange) {
            dismiss();
            if (listener != null) {
                listener.clickChange();
            }
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickDelete();

        void clickChange();
    }
}
